import BaseScene from "../../base/Script/core/BaseScene";
import BundleManager from "../../base/Script/core/BundleManager";
import PopupManager from "../../base/Script/core/PopupManager";
import LogUtil from "../../base/Script/utils/LogUtil";
import NodeUtil from "../../base/Script/utils/NodeUtil";
import BattleRoleUi from "./BattleRoleUi";
import { WinState } from "./CFCommon";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BattleUi extends BaseScene {

    @property({ type: [cc.Node] })
    owners: Array<cc.Node> = [];

    @property({ type: [cc.Node] })
    enemys: Array<cc.Node> = [];

    @property(cc.Prefab)
    roleUI: cc.Prefab = null;

    private ownerCtrls:Array<BattleRoleUi> = [];
    private enemyCtrls:Array<BattleRoleUi> = [];
    private frameNum:number = 1;
    private maxFrameNum:number = 30;
    private isEnd:boolean = false;
    private ownerIdx = 0;
    private enemyIdx = 0;
    private maxIdx = 4;
    private winState:WinState = WinState.none;

    start () {
        this.frameNum = 1;

        for(let i=0;i<this.owners.length;i++){
            let node = cc.instantiate(this.roleUI);
            this.owners[i].addChild(node);
            let ownerCtrl = node.getComponent(BattleRoleUi);
            ownerCtrl.init(true,{hp:100,power:0,atk:10,spinePath:"spine/heroFight10520101"});
            this.ownerCtrls[i] = ownerCtrl
        }

        for(let i=0;i<this.owners.length;i++){
            let node = cc.instantiate(this.roleUI);
            this.enemys[i].addChild(node);
            let enemyCtrl = node.getComponent(BattleRoleUi);
            enemyCtrl.init(false,{hp:100,power:0,atk:10,spinePath:"spine/heroFight10520110"});
            this.enemyCtrls[i] = enemyCtrl
        }
        
        let action = ['skill1', 'skill2','run', 'idle', 'die'];
        let btn1 = NodeUtil.getChildByName(this.node,'btn1');
        btn1.on(cc.Node.EventType.TOUCH_END, ()=>{
            LogUtil.log('btn1 click>>');
            if(this.frameNum==1){
                this.run();
            }
        }, this);
    }

    onDestroy(): void {
        super.onDestroy();
        BundleManager.ins().releaseAll('CardFight');
    }

    checkWin(isOwner:boolean){
        let win = true
        if(isOwner){
            for(let i=0;i<this.enemyCtrls.length;i++){
                if(this.enemyCtrls[i].getHp()>0){
                    win = false
                    break;
                }
            }
            if(win){
                this.winState = WinState.win;
            }
        }else{
            for(let i=0;i<this.ownerCtrls.length;i++){
                if(this.ownerCtrls[i].getHp()>0){
                    win = false
                    break;
                }
            }
            if(win){
                this.winState = WinState.lose;
            }
        }
        if(win){
            let tps=["","我方胜利","敌方胜利"];
            this.scheduleOnce(()=>{
                PopupManager.show('prefabs/PopBox', {
                    title: '提示',
                    content: tps[this.winState],
                    confirmCallback: () => {
                    }
                })
            },1);
        }
    }

    ownersAtk(){
        let next = ()=>{
            if(this.winState != WinState.none) return;
            this.ownerIdx++;
            if(this.ownerIdx>this.maxIdx){
                this.frameNum++;
                this.run();
            }else{
                this.ownersAtk();
            }
        }
        if(this.ownerCtrls[this.ownerIdx].getHp()>0){
            for(let i=0;i<this.enemyCtrls.length;i++){
                if(this.enemyCtrls[i].getHp()>0){
                    if(this.ownerCtrls[this.ownerIdx].getPower()>=1){
                        this.ownerCtrls[this.ownerIdx].doAction("skill2");
                        this.scheduleOnce(()=>{
                            this.ownerCtrls[this.ownerIdx].updatePower(-1);
                            this.enemyCtrls[i].updateHp(100);
                            if(this.enemyCtrls[i].getHp()<=0){
                                this.enemyCtrls[i].onDead();
                                this.checkWin(true);
                            }
                            this.scheduleOnce(()=>{
                                next();
                            },1)
                        },1);
                    }else{
                        this.ownerCtrls[this.ownerIdx].doAction("skill1");
                        this.scheduleOnce(()=>{
                            this.ownerCtrls[this.ownerIdx].updatePower(1);
                            this.enemyCtrls[i].updateHp(30);
                            if(this.enemyCtrls[i].getHp()<=0){
                                this.enemyCtrls[i].onDead();
                                this.checkWin(true);
                            }
                            this.scheduleOnce(()=>{
                                next();
                            },1)
                        },0.5);
                    }
                    break;
                }
            }
        }else{
            next();
        }
    }

    enemysAtk(){
        let next = ()=>{
            if(this.winState != WinState.none) return;
            this.enemyIdx++;
            if(this.enemyIdx>this.maxIdx){
                this.frameNum++;
                this.run();
            }else{
                this.enemysAtk();
            }
        }
        if(this.enemyCtrls[this.enemyIdx].getHp()>0){
            for(let i=0;i<this.ownerCtrls.length;i++){
                if(this.ownerCtrls[i].getHp()>0){
                    if(this.enemyCtrls[this.enemyIdx].getPower()>=1){
                        this.enemyCtrls[this.enemyIdx].doAction("skill2");
                        this.scheduleOnce(()=>{
                            this.enemyCtrls[this.enemyIdx].updatePower(-1);
                            this.ownerCtrls[i].updateHp(100);
                            if(this.ownerCtrls[i].getHp()<=0){
                                this.ownerCtrls[i].onDead();
                                this.checkWin(false);
                            }
                            this.scheduleOnce(()=>{
                                next();
                            },1)
                        },1);
                    }else{
                        this.enemyCtrls[this.enemyIdx].doAction("skill1");
                        this.scheduleOnce(()=>{
                            this.enemyCtrls[this.enemyIdx].updatePower(1);
                            this.ownerCtrls[i].updateHp(50);
                            if(this.ownerCtrls[i].getHp()<=0){
                                this.ownerCtrls[i].onDead();
                                this.checkWin(false);
                            }
                            this.scheduleOnce(()=>{
                                next();
                            },1)
                        },0.5);
                    }
                    break;
                }
            }
        }else{
            next();
        }
    }

    run(){
        this.ownerIdx = 0;
        this.enemyIdx = 0;
        if(this.frameNum>=this.maxFrameNum){
            this.winState = WinState.timeout;
            this.scheduleOnce(()=>{
                PopupManager.show('prefabs/PopBox', {
                    title: '提示',
                    content: '游戏回合超过',
                    confirmCallback: () => {
                    }
                })
            },1);
            return;
        }
        if(this.frameNum%2==0){
            this.enemysAtk()
        }else{
            this.ownersAtk()
        }
    }
}
